package com.tianbo.controller.json;

import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.TKpiCargoWaybill;
import com.tianbo.model.TKpiCargoWaybillExample;
import com.tianbo.model.TKpiCargoWaybillExample.Criteria;
import com.tianbo.service.KPICargoBillServcie;
import com.tianbo.util.dao.Page;
import com.tianbo.util.dao.Totals;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/kpi")
public class KPIJsonController extends BaseController{
    @Autowired
    KPICargoBillServcie kpiService;

    /**
     * 查找所用用户控制器方法
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "list.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public ModelAndPage kpiList(@ModelAttribute Page frontPage, @ModelAttribute TKpiCargoWaybill kpi) throws Exception{

        if(frontPage.getBegin()>=0){
            defaultDataStart = frontPage.getBegin();
        }
        if (frontPage.getLength()>0){
            defaultPerPage = frontPage.getLength();
        }

        //example
        //调用mapper类中的selectByExample方法，如果传入类型为null，则表示无条件查找
        TKpiCargoWaybillExample example = new TKpiCargoWaybillExample();
        Criteria criteria = example.createCriteria();
        Criteria criteria2= example.createCriteria();
        Criteria criteria3= example.createCriteria();
        Criteria criteria4= example.createCriteria();

        //根据国内国际搜索
        if (checkPropertyNull(kpi.getDomint())){
            criteria.andDomintEqualTo(kpi.getDomint());
            criteria2.andDomintEqualTo(kpi.getDomint());
            criteria3.andDomintEqualTo(kpi.getDomint());
            criteria4.andDomintEqualTo(kpi.getDomint());
        }

        //根据运单前缀，前缀有主单的情况是三位前缀，分单的情况是主单号
        if(checkPropertyNull(kpi.getWaybillpre())){
            criteria.andWaybillpreEqualTo(kpi.getWaybillpre());
            criteria2.andWaybillpreEqualTo(kpi.getWaybillpre());
            criteria3.andWaybillpreEqualTo(kpi.getWaybillpre());
            criteria4.andWaybillpreEqualTo(kpi.getWaybillpre());
        }
        //根据运单类型
        if(checkPropertyNull(kpi.getWaybilltype())){
            criteria.andWaybilltypeEqualTo(kpi.getWaybilltype());
            criteria2.andWaybilltypeEqualTo(kpi.getWaybilltype());
            criteria3.andWaybilltypeEqualTo(kpi.getWaybilltype());
            criteria4.andWaybilltypeEqualTo(kpi.getWaybilltype());
        }

        //根据代理人id
        if(checkPropertyNull(kpi.getAgentid())){
            criteria.andAgentidEqualTo(kpi.getAgentid());
            criteria2.andAgentidEqualTo(kpi.getAgentid());
            criteria3.andAgentidEqualTo(kpi.getAgentid());
            criteria4.andAgentidEqualTo(kpi.getAgentid());
        }

        //根据货主
        if(checkPropertyNull(kpi.getShprname())){
            criteria.andShprnameEqualTo(kpi.getShprname());
            criteria2.andAgentidEqualTo(kpi.getAgentid());
            criteria3.andAgentidEqualTo(kpi.getAgentid());
            criteria4.andAgentidEqualTo(kpi.getAgentid());
        }
        //根据货物id
        if(checkPropertyNull(kpi.getProductid())){
            criteria.andProductidEqualTo(kpi.getProductid());
            criteria2.andAgentidEqualTo(kpi.getAgentid());
            criteria3.andAgentidEqualTo(kpi.getAgentid());
            criteria4.andAgentidEqualTo(kpi.getAgentid());
        }
        //根据货物名称
        if(checkPropertyNull(kpi.getProductname())){
            criteria.andProductnameEqualTo(kpi.getProductname());
            criteria2.andProductnameEqualTo(kpi.getProductname());
            criteria3.andProductnameEqualTo(kpi.getProductname());
            criteria4.andProductnameEqualTo(kpi.getProductname());
        }

        //根据进口
        if(checkPropertyNull(kpi.getImp())){
            criteria.andImpEqualTo(kpi.getImp());
            criteria2.andImpEqualTo(kpi.getImp());
            criteria3.andImpEqualTo(kpi.getImp());
            criteria4.andImpEqualTo(kpi.getImp());
        }
        //根据出口
        if(checkPropertyNull(kpi.getExp())){
            criteria.andExpEqualTo(kpi.getExp());
            criteria2.andExpEqualTo(kpi.getExp());
            criteria3.andExpEqualTo(kpi.getExp());
            criteria4.andExpEqualTo(kpi.getExp());
        }
        //根据时间段-年
        if(kpi.getCuryear()!=null&&kpi.getEndyear()!=null){
            criteria.andCuryearBetween(kpi.getCuryear(),kpi.getEndyear());
            criteria2.andCuryearBetween(kpi.getCuryear(),kpi.getEndyear());
            criteria3.andCuryearBetween(kpi.getCuryear(),kpi.getEndyear());
            criteria4.andCuryearBetween(kpi.getCuryear(),kpi.getEndyear());
        }

        //根据时间段-月
        if(kpi.getCurmonth()!=null&&kpi.getEndmonth()!=null){
            criteria.andCurmonthBetween(kpi.getCurmonth(),kpi.getEndmonth());
            criteria2.andCurmonthBetween(kpi.getCurmonth(),kpi.getEndmonth());
            criteria3.andCurmonthBetween(kpi.getCurmonth(),kpi.getEndmonth());
            criteria4.andCurmonthBetween(kpi.getCurmonth(),kpi.getEndmonth());
        }

        //根据时间段-日
        if(kpi.getCurweek()!=null&&kpi.getEndweek()!=null){
            criteria.andCurweekBetween(kpi.getCurweek(),kpi.getEndweek());
            criteria2.andCurweekBetween(kpi.getCurweek(),kpi.getEndweek());
            criteria3.andCurweekBetween(kpi.getCurweek(),kpi.getEndweek());
            criteria4.andCurweekBetween(kpi.getCurweek(),kpi.getEndweek());
        }


        //根据承运人搜索
        if (checkPropertyNull(kpi.getAirline1())){
            String carrier = kpi.getAirline1();

            criteria.andAirline1EqualTo(carrier);
            criteria2.andAirline2EqualTo(carrier);
            criteria3.andAirline3EqualTo(carrier);
            criteria4.andAirline4EqualTo(carrier);



            example.or(criteria2);
            example.or(criteria3);
            example.or(criteria4);
        }

        //example.setOrderByClause("user_id"); //设置order 字段

        //没加入分页前的搜索总数据数
        int count = kpiService.countByExample(example);
        BigDecimal weight = kpiService.sumWeightByExample(example);
        BigDecimal pcs= kpiService.sumPcsByExample(example);
        BigDecimal vol= kpiService.sumVolByExample(example);
        BigDecimal hbillqty= kpiService.sumHbillqtyByExample(example);
        Totals totals = new Totals(weight,pcs,vol,hbillqty);

        Page page = new Page(defaultDataStart,defaultPerPage);
        page.setCount(count);

        example.setPage(page); //oracle 用这个

        List<TKpiCargoWaybill> kpilist = kpiService.selectByExample(example);

        ModelAndPage<TKpiCargoWaybill> modelAndPage = new ModelAndPage(kpilist,page,totals);

        return modelAndPage;
    }
}
